package com.huawei.mpchartexample;

import com.huawei.mpchartlib.charts.BarChart;
import com.huawei.mpchartlib.charts.BubbleChart;
import com.huawei.mpchartlib.components.Legend;
import com.huawei.mpchartlib.components.XAxis;
import com.huawei.mpchartlib.components.YAxis;
import com.huawei.mpchartlib.data.BubbleData;
import com.huawei.mpchartlib.data.BubbleDataSet;
import com.huawei.mpchartlib.data.BubbleEntry;
import com.huawei.mpchartlib.interfaces.datasets.IBubbleDataSet;
import com.huawei.mpchartlib.utils.ColorTemplate;
import com.huawei.mpchartlib.utils.MPPointF;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;

import java.util.ArrayList;

public class BubbleChartAbility extends Ability {

    private BubbleChart chart;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_bubblechart);

        chart = (BubbleChart) findComponentById(ResourceTable.Id_bubbleChart);

        chart.setDrawGridBackground(false);
        chart.setTouchEnabled(false);

        Legend l = chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);

        YAxis yl = chart.getAxisLeft();
        yl.setSpaceTop(30f);
        yl.setSpaceBottom(30f);
        yl.setDrawZeroLine(false);

        chart.getAxisRight().setEnabled(false);

        XAxis xl = chart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);

        setData(10, 50);
        chart.invalidate();
        
        // draw chart
        Component.DrawTask drawTask = new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                chart.onDraw(canvas);
            }
        };
        chart.addDrawTask(drawTask);
    }

    private void setData(int count, float range) {
        ArrayList<BubbleEntry> values1 = new ArrayList<>();
        ArrayList<BubbleEntry> values2 = new ArrayList<>();
        ArrayList<BubbleEntry> values3 = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            values1.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range)));
            values2.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range)));
            values3.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range)));
        }

        // create a dataset and give it a type
        BubbleDataSet set1 = new BubbleDataSet(values1, "DS 1");
        set1.setDrawIcons(false);
        set1.setColor(ColorTemplate.COLORFUL_COLORS[0], 130);
        set1.setDrawValues(true);

        BubbleDataSet set2 = new BubbleDataSet(values2, "DS 2");
        set2.setDrawIcons(false);
        set2.setIconsOffset(new MPPointF(0, 15));
        set2.setColor(ColorTemplate.COLORFUL_COLORS[1], 130);
        set2.setDrawValues(true);

        BubbleDataSet set3 = new BubbleDataSet(values3, "DS 3");
        set3.setColor(ColorTemplate.COLORFUL_COLORS[2], 130);
        set3.setDrawValues(true);

        ArrayList<IBubbleDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1); // add the data sets
        dataSets.add(set2);
        dataSets.add(set3);

        // create a data object with the data sets
        BubbleData data = new BubbleData(dataSets);
        data.setDrawValues(false);
        data.setValueTextSize(8f);
        data.setValueTextColor(Color.WHITE.getValue());
        data.setHighlightCircleWidth(1.5f);

        chart.setData(data);
    }
}
